package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.MchntKeyInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.MchntKeyInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.MchntKeyInfo;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.MchntKeyInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntKeyInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntKeyInfoQueryResponse;
import cc.rengu.igas.share.common.enums.RespCodeEnum;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * 商户密钥查询服务
 * Created by wangcheng on 2021/10/22 11:39
 */
public class BompMchntKeyInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntKeyInfoQueryRequest mchntKeyInfoQueryRequest = new MchntKeyInfoQueryRequest();
        ConvertUtil.convertOutput(mchntKeyInfoQueryRequest);
        return mchntKeyInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntKeyInfoQueryRequest mchntKeyInfoQueryRequest = (MchntKeyInfoQueryRequest) request;
        String instId = mchntKeyInfoQueryRequest.getHeader().getInstId();
        String mchntNo = mchntKeyInfoQueryRequest.getMchntNo();
        String mchntAppId = mchntKeyInfoQueryRequest.getMchntAppId();
        String keyType = mchntKeyInfoQueryRequest.getKeyType();
        if (StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(mchntAppId) || StringUtil.isEmptyOrNull(keyType)) {
            rglog.error("未上送商户号!");
            throw new BizException(RespCodeEnum.INVALID_REQEST_PARAM.getRespCode(), RespCodeEnum.INVALID_REQEST_PARAM.getRespDesc());
        }
        MchntKeyInfoQueryResponse mchntKeyInfoQueryResponse = new MchntKeyInfoQueryResponse();
        mchntKeyInfoQueryResponse.setHeader(mchntKeyInfoQueryRequest.getHeader());
        MchntKeyInfoMapper mchntKeyInfoMapper = new MchntKeyInfoMapperImpl();
        MchntKeyInfo mchntKeyInfo = mchntKeyInfoMapper.selectMchntKeyInfoByPrimaryKey(instId, mchntNo, mchntAppId, keyType);
        if (null != mchntKeyInfo) {
            MchntKeyInfoBean mchntKeyInfoBean = new MchntKeyInfoBean();
            BeanUtil.beanCopy(mchntKeyInfo, mchntKeyInfoBean);
            mchntKeyInfoQueryResponse.setMchntKeyInfo(mchntKeyInfoBean);
        }

        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntKeyInfoQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntKeyInfoQueryResponse mchntKeyInfoQueryResponse = (MchntKeyInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntKeyInfoQueryResponse);
    }
}
